package com.weiz.thread.threadpool;

import com.weiz.config.InitConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ThreadPoolExecutor;

@RestController
public class ThreadPoolController {
    private static final Logger log = LoggerFactory.getLogger(ThreadPoolController.class);
    @Autowired
    @Qualifier("threadPoolExecutorNormal")
    private ThreadPoolExecutor threadPoolExecutor;

    //http://localhost:8090/rabbitmq/tpstatus
    @GetMapping("/tpstatus")
    public String getThreadPoolStatus() {
        threadPoolExecutor.execute(()->{
            SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            log.info("tpstatus"+Thread.currentThread().getName() + sdf.format(new Date()));
        });
        StringBuilder status = new StringBuilder();
        status.append("Core Pool Size: ").append(threadPoolExecutor.getCorePoolSize()).append("\n");
        status.append("Maximum Pool Size: ").append(threadPoolExecutor.getMaximumPoolSize()).append("\n");
        status.append("Current Pool Size: ").append(threadPoolExecutor.getPoolSize()).append("\n");
        status.append("Active Thread Count: ").append(threadPoolExecutor.getActiveCount()).append("\n");
        status.append("Task Count: ").append(threadPoolExecutor.getTaskCount()).append("\n");
        status.append("Completed Task Count: ").append(threadPoolExecutor.getCompletedTaskCount()).append("\n");
        status.append("Queue Size: ").append(threadPoolExecutor.getQueue().size()).append("\n");
        return status.toString();
    }
}